<template>
  <div id="coupons" class="w">
    <div class="title-coupons">
      <span></span>
      <h3>精选好券</h3>
      <span></span>
    </div>
    <div v-if="!coupons || !coupons.data.length" class="no-coupons">暂无优惠券</div>
    <div v-else class="list-coupons">
      <div
        v-for="(coupon, index) in coupons.data"
        :key="index"
        class="item-coupon"
        :class="[coupon.create_num === coupon.received_num && 'brought-out']"
      >
        <div class="img-coupon">
          <img src="">
        </div>
        <div class="info-coupon">
          <div class="price-coupon">
            <em>¥</em>
            <strong>{{ coupon.coupon_price }}</strong>
            <span class="limit-coupon">满{{ coupon.coupon_threshold_price }}可用</span>
          </div>
          <div class="range-coupon">
            <span :title="coupon.title">{{ coupon.title }}</span>
          </div>
          <div class="shop-name">
            <div :title="coupon.seller_name" v-if="coupon.use_scope">
              仅限
              <span v-if="coupon.use_scope === 'ALL'">全平台</span>
              <span v-if="coupon.use_scope === 'CATEGORY'">品类</span>
              <span v-if="coupon.use_scope === 'SOME_GOODS'">部分商品</span>
              使用
            </div>
            <span :title="coupon.seller_name" v-else>仅限 <nuxt-link :to="'/shop/' + coupon.seller_id">{{ coupon.seller_name }}</nuxt-link> 店铺使用</span>
          </div>
        </div>
        <div class="circle-coupon">
          <i></i><i></i>
        </div>
        <div class="opt-coupon" @click="receiveCoupon(coupon)">
          <div class="btn-opt-coupon">
            <a v-if="coupon.create_num === coupon.received_num"><b></b><span class="txt">已领完</span></a>
            <a v-else href="javascript:;"><b></b><span class="txt">立即领取</span></a>
          </div>
        </div>
      </div>
    </div>
    <div class="coupon-pagin">
      <el-pagination
        v-if="coupons"
        @current-change="handleCurrentPageChange"
        :current-page.sync="coupons.page_no"
        :page-size="coupons.page_size"
        layout="total, prev, pager, next"
        :total="coupons.data_total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import { Pagination } from 'element-ui'
  Vue.use(Pagination)
  import * as API_promotions from '@/api/promotions'
  import * as API_Members from '@/api/members'
  import Storage from '@/utils/storage'
  export default {
    name: 'coupons',
    head() {
      return {
        title: `优惠券市场-${this.site.title}`
      }
    },
    data() {
      return {
        loading: false,
        finished: false,
        params: {
          page_no: 1,
          page_size: 10
        },
        coupons: ''
      }
    },
    mounted() {
      this.GET_Coupons()
    },
    methods: {
      /** 加载数据 */
      handleCurrentPageChange(page_no) {
        this.params.page_no = page_no
        this.GET_Coupons()
      },
      /** 领取优惠券 */
      receiveCoupon(coupon) {
        if (!Storage.getItem('refresh_token')) {
          this.$confirm('您还未登录，要现在去登录吗？', () => {
            this.$router.push({ path: '/login', query: { forward: `${this.$route.path}`} })
          });
          return false
        }
        API_Members.receiveCoupons(coupon.coupon_id).then(() => {
          this.$message.success('领取成功！')
        })
      },
      /** 获取优惠券列表 */
      GET_Coupons() {
        API_promotions.getAllCoupons(this.params).then(response => {
          this.coupons = response
        })
      }
    }
  }
</script>

<style type="text/scss" lang="scss" scoped>
  @import "../assets/styles/color";
  .title-coupons {
    height: 35px;
    line-height: 1;
    margin: 20px auto;
    text-align: center;
    span {
      display: inline-block;
      width: 35px;
      height: 18px;
      background: url() 0 0 no-repeat;
      overflow: hidden;
      vertical-align: middle;
    }
    h3 {
      display: inline-block;
      margin: 0 5px;
      color: #111;
      font-size: 18px;
      line-height: 35px;
      vertical-align: middle;
    }
  }
  .list-coupons {
    display: block;
    overflow: hidden;
  }
  .item-coupon {
    position: relative;
    display: block;
    float: left;
    height: 145px;
    border: 1px solid #f2f2f2;
    background: #fff;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.04);
    margin: 0 20px 20px 0;
    width: 388px;
    overflow: visible;
    &.brought-out {
      filter: grayscale(100%);
    }
    &:nth-child(3n) {
      margin-right: 0;
    }
    &:hover .img-coupon {
      transform: translateX(5px);
    }
    .img-coupon {
      float: left;
      width: 104px;
      height: 104px;
      margin: 20px;
      border: 1px solid #f1f2f6;
      transition: all .2s ease-in-out,opacity 1s;
      img {
        width: 100px;
        height: 100px;
      }
    }
    .info-coupon {
      position: relative;
      float: left;
      z-index: 2;
      width: 190px;
      height: 120px;
      padding: 10px 0 15px;
      color: #333;
      outline: 0;
      .price-coupon {
        width: 190px;
        height: 45px;
        margin-bottom: 5px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      em {
        display: inline-block;
        font: 400 18px arial;
        vertical-align: top;
        margin: 3px 3px 0 0;
        color: $color-main
      }
      strong {
        display: inline-block;
        font: 400 30px arial;
        vertical-align: top;
        color: $color-main;
      }
      .limit-coupon {
        padding: 0 5px;
        color: $color-main;
        background: #fff4ec;
        display: inline-block;
        font-size: 12px;
        margin: 12px 0 0 5px;
        vertical-align: top;
      }
      .range-coupon {
        height: 36px;
        line-height: 18px;
        overflow: hidden;
        font-size: 12px;
        font-weight: 700;
        margin: 0 0 15px;
        cursor: default;
      }
    }
    .circle-coupon {
      i {
        position: absolute;
        z-index: 3;
        display: block;
        width: 18px;
        height: 9px;
        background: url() no-repeat;
        overflow: hidden;
        right: 42px;
        &:first-child {
          top: -1px;
          background-position: 0 -9px;
        }
        &:last-child {
          bottom: -1px;
          background-position: 0 0;
        }
      }
    }
    .opt-coupon {
      .btn-opt-coupon {
        position: absolute;
        top: 0;
        right: 0;
        width: 52px;
        height: 145px;
        background-color: $color-main;
        a {
          display: block;
          width: 52px;
          height: 100%;
          line-height: 22px;
          text-align: center;
          font-size: 14px;
          color: #fff;
        }
        b {
          position: absolute;
          z-index: 2;
          top: 0;
          left: -1px;
          display: block;
          width: 3px;
          height: 100%;
          background: url() top left repeat-y;
        }
        span {
          position: relative;
          display: block;
          top: 25px;
          width: 20px;
          margin: 0 auto;
          font-size: 16px;
        }
      }
    }
  }
  .no-coupons {
    text-align: center;
    font-size: 18px;
    font-weight: 400;
    line-height: 100px;
  }
  .coupon-pagin {
    text-align: right;
    clear: both;
    padding: 20px 0;
  }
</style>
